package com.netposa.datawarehouse.bodysearch.convert;

import com.netposa.datawarehouse.bodysearch.web.model.BodyDownloadCondition;
import com.netposa.datawarehouse.bodysearch.web.vo.BodyVO;
import com.netposa.library.api.IDictionaryCache;
import com.netposa.library.model.Dictionary;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;

public class DeepLDictionaryConvertImpl implements DictionaryConvert {
   private IDictionaryCache dictionaryCache;

   public DeepLDictionaryConvertImpl(IDictionaryCache dictionaryCache) {
      this.dictionaryCache = dictionaryCache;
   }

   public String[] convert(BodyVO body, BodyDownloadCondition condition) {
      String gender = "";
      String upperType = "";
      String upperCol = "";
      String lowerType = "";
      String lowCol = "";
      String bodyAppendageStr = "";
      String headFlag = "";
      String deviceType = "";
      Dictionary dictionary;
      if (body.getGender() != null) {
         dictionary = this.dictionaryCache.get("Sex_VM", body.getGender().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            gender = dictionary.getValue();
         }
      }

      if (body.getUpperPattern() != null) {
         dictionary = this.dictionaryCache.get("CoatVein_VM", body.getUpperPattern().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            upperType = dictionary.getValue();
         }
      }

      if (body.getUpperColor() != null) {
         dictionary = this.dictionaryCache.get("UnderClothesColor_VM", body.getUpperColor().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            upperCol = dictionary.getValue();
         }
      }

      if (body.getLowerType() != null) {
         dictionary = this.dictionaryCache.get("UnderClothesType_VM", body.getLowerType().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            lowerType = dictionary.getValue();
         }
      }

      if (body.getLowerColor() != null) {
         dictionary = this.dictionaryCache.get("UnderClothesColor_VM", body.getLowerColor().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            lowCol = dictionary.getValue();
         }
      }

      if (body.getHeadMarker() != null) {
         dictionary = this.dictionaryCache.get("HeadFlag_VM", body.getHeadMarker().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            headFlag = dictionary.getValue();
         } else {
            headFlag = "未识别";
         }
      }

      if (body.getNodeType() != null) {
         switch (body.getNodeType()) {
            case "camera":
               deviceType = "摄像机";
               break;
            case "monitor":
               deviceType = "车辆卡口";
               break;
            case "face_monitor":
               deviceType = "人脸卡口";
               break;
            case "rfid":
               deviceType = "RFID";
               break;
            case "wifi":
               deviceType = "WIFI";
               break;
            case "lamppost":
               deviceType = "灯杆";
         }
      }

      Integer bodyAppendageInt = body.getBodyAppendage();
      if (bodyAppendageInt != null) {
         String bodyAppendage = "";
         if (bodyAppendageInt != -1 && bodyAppendageInt != 0) {
            bodyAppendage = this.generateCode(bodyAppendageInt);
         } else {
            bodyAppendage = body.getBagType().toString();
         }

         StringBuffer bodyAppendageBuffer = new StringBuffer();
         String[] var14 = bodyAppendage.split(",");
         int var15 = var14.length;

         for(int var16 = 0; var16 < var15; ++var16) {
            String bodyAppendageTemp = var14[var16];
            Dictionary dictionary2 = this.dictionaryCache.get("BagType_VM", bodyAppendageTemp);
            if (dictionary2 != null && StringUtils.isNotBlank(dictionary2.getValue())) {
               bodyAppendageBuffer.append("/").append(dictionary2.getValue());
            }
         }

         if (bodyAppendageBuffer.length() > 0) {
            bodyAppendageStr = bodyAppendageBuffer.substring(1).toString();
         }
      }

      Date date = new Date(body.getAbsTime());
      SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String absTime = sdFormat.format(date);
      String imgUrl = condition.getImgFlag() == "1" ? body.getTraitImg() : body.getSceneImg();
      String[] row = new String[]{absTime, body.getDeviceName(), gender, upperType, upperCol, lowerType, lowCol, condition.getDownloadPics() ? imgUrl : "", bodyAppendageStr, deviceType, headFlag};
      return row;
   }

   private String generateCode(int value) {
      String str = "";

      for(int i = 1; i <= 31; ++i) {
         int m = 1 << i - 1;
         if ((m & value) == m) {
            str = str + i + ",";
         }
      }

      return str.substring(0, str.length() - 1);
   }
}
